//对于自定义类型在比较的时候，莫若要进行大小的比较，那么只需要实现comparable接口
// 并重写compareTo

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Random;

class comp implements Comparable<comp>{
    int val;
    char headChar;

    public comp(int val, char headChar) {
        this.val = val;
        this.headChar = headChar;
    }

    @Override
    public int compareTo(comp o) {//比较的是字符
        return this.headChar - o.headChar;
    }
}

//基于比较器的比较，comparator接口 重写其中的compare方法
class comp_tor implements Comparator<comp> {

    @Override
    public int compare(comp o1, comp o2) {
        return o1.val- o2.val;
    }
}

class down implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}
class up implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o1-o2;
    }
}
class tet{
    int val;

    public tet(int val) {
        this.val = val;
    }
}
public class comparable_ {

    public static void main(String[] args) {
        down t = new down();
        up tt = new up();
//        PriorityQueue<Integer> one = new PriorityQueue<>(t);//大
        PriorityQueue<Integer> one = new PriorityQueue<>(tt);//小
        //
        int[] array = new int[10];
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            array[i] = random.nextInt(10);
        }
        System.out.println(Arrays.toString(array));
        for (int i = 0; i < 10; i++) {
            //建立大根对
            one.offer(array[i]);
        }
//        System.out.println(one.peek());
        Integer[] tmp = new Integer[10];
        one.toArray(tmp);
        System.out.println(Arrays.toString(Arrays.stream(tmp).toArray()));
        Object[] oo = {1, 3, 5, 4};
        Integer[] tmp1 = new Integer[10];
        tmp1 = Arrays.copyOfRange(oo, 0, 4, Integer[].class);
        System.out.println(Arrays.toString(Arrays.stream(tmp1).toArray()));
        //将object数组转换为Integer数组
        Integer[] aa = Arrays.copyOfRange(one.toArray(), 0, array.length, Integer[].class);//

        System.out.println(Arrays.toString(Arrays.stream(aa).toArray()));

        //        Integer[] oo = (Integer[]) one.toArray();
//        System.out.println(Arrays.toString(Arrays.stream(oo).toArray()));
//        System.out.println(Arrays.toString(array));
    }
    public static void main2(String[] args) {
        Random random = new Random();
        Integer[] array = new Integer[10];
        for (int i = 0; i < 10; i++) {
            array[i] = random.nextInt(10);
        }
        down tt = new down();
        up ttt = new up();
        System.out.println(Arrays.toString(Arrays.stream(array).toArray()));
        //可以通过传一个比较器去实现对数组排序顺序的控制
        Arrays.sort(array,ttt);
        System.out.println(Arrays.toString(Arrays.stream(array).toArray()));
    }
    public static void main1(String[] args) {
        comp one = new comp(1, 'a');
        comp two = new comp(2, 'b');

        comp_tor cmp = new comp_tor();
        System.out.println(one.compareTo(two));
        System.out.println(cmp.compare(one, two));

        tet three = new tet(1);
        tet four = new tet(2);
    }

}
